// pages/list/list.js
var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    pagesize:20, 
    start:0,
    goods:[],
    headimg,
    showmodal:0,
    buysrc:'',
    buyprice:'',
    buystore:'',
    goodsnum:'',
    count: 1,
    speccont:'',
    indexsel:0,
    goodsdata:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    console.log(options,'optionsoptionsoptions')

    wx.showLoading({
      title: '加载中',
    })
    let {id} = options;
    that.setData({id},()=>{
      that.getdata()
    })
   
  },
  getdata(){
    let { id, pagesize, start, goods } = that.data, promotionprice = 0;
    api.getmainpicpath({ pagesize, start, cid: id }, res => {
      wx.hideLoading()
      if (res.data.issuccess == 1) {
        let newlist = [];
        res.data.list.forEach((x, i) => {
          console.log(x, 'xxxx')
          // let newsrc = headimg + x.topimg
          x['newprice'] = (x.promotionprice / 100).toFixed(2);
          newlist.push(x)
          // console.log(newsrc,'newsrcnewsrcnewsrc')
          // goods.push([x, newsrc, promotionprice])
        })
        goods = goods.concat(newlist)
        wx.setNavigationBarTitle({
          title: res.data.name,
        })
        that.setData({ goods, reachbtm: goods.length == res.data.total })
      }
    })
  },
  jumptogoodsdetail(e){
    let { id } = e.currentTarget.dataset;
    console.log(e, 'idid')
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?id=' + id,
    })
  },   
  //显示购物车弹窗
  showcarmodel(e){
    let {id,item} = e.currentTarget.dataset,{buysrc,buyprice,buystore,goodsnum,usertype,count} = that.data
    that.setData({
      buyprice: (item.tygprice/100).toFixed(2),
      price: (item.promotionprice/100).toFixed(2),
      totalscore:item.tygscore,
      score:item.tygscore,
      count:1,
      anim: 'sizewrap 0.3s',
      goodsdata:item
    })
    that.setData({
      showmodal:1,
    },()=>{      
   
    })
  },
  closechose() {
    that.setData({
      showmodal: !1,
      anim: ''
    })
  },

  //加入购物车
  addshopcar(){
    let { count,goodsdata }=that.data,paynum='',cartids=wx.getStorageSync('cartids')
    paynum=count
    let data={  paynum,type:'GOODS',goodsid:goodsdata.id}
      api.addcart(data,res=>{
        if(res.data.issuccess == 1){
          wx.showToast({
            title: '加入购物车成功',
            icon: 'none',
            duration: 2000,
            success(){
              console.log('cartids');
              cartids.push(goodsdata)
              app.badege(cartids)
              wx.setStorageSync("cartids",cartids)
            }
          })
          that.setData({
            showmodal:0
          })
        }else{
          wx.showToast({
            title: '加入购物车失败',
            icon: 'none',
            duration: 2000
          })
        }
        })
    // }
  },
    // 选择规格
    specitemsel(e){
      let {index,speccont}=e.currentTarget.dataset
      that.setData({indexsel:index,speccont})
    },

  //关闭购物车弹窗
  closemodle(){
    that.setData({
      showmodal: 0,
    })
  },


  //购物车数量

  counttap(e) {
    let {
      count,buystore,goodsdata
    } = that.data
    if (e.currentTarget.dataset.code == 1) {
     if(count<goodsdata.kc){
       count++
     }
     else{
       wx.showToast({
         title: '商品库存不足',
         icon: 'none',
         duration: 2000
       })
     }
    } else {
      if (count > 1) {
        count--
      } else {
        wx.showToast({
          title: '至少添加一件商品',
          icon: 'none',
          duration: 2000
        })
      }
    }
    let buyprice =(Number(that.data.price)*count).toFixed(2)
    that.setData({
      buyprice,
      count
    })
    // that.setData({
    //   count
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    that.setData({ start: that.data.start + that.data.pagesize }, () => {
      // console.log(that.data.start,'start')
      if (that.data.reachbtm) {
        return
      }
      that.getdata()
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})